<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="70vw" :fullscreen="true">
    <el-form size="large" ref="formRef" :model="formData" :rules="formRules" label-width="100px"
      v-loading="formLoading">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="企业名称" prop="enterpriseName">
            <el-input v-model="formData.enterpriseName" placeholder="请输入企业名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="简称" prop="simpleName">
            <el-input v-model="formData.simpleName" placeholder="请输入简称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="信用代码" prop="creditCode">
            <el-input v-model="formData.creditCode" placeholder="请输入信用代码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="企业代号" prop="enterpriseCode">
            <el-input v-model="formData.enterpriseCode" placeholder="请输入企业代号" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="法人代表" prop="legalRepresentative">
            <el-input v-model="formData.legalRepresentative" placeholder="请输入法人代表" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="公司官网" prop="companyWebsite">
            <el-input v-model="formData.companyWebsite" placeholder="请输入公司官网" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系人" prop="contactPerson">
            <el-input v-model="formData.contactPerson" placeholder="请输入联系人" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系电话" prop="mobilePhone">
            <el-input v-model="formData.mobilePhone" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="固定电话" prop="landlinePhone">
            <el-input v-model="formData.landlinePhone" placeholder="请输入固定电话" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="传真号码" prop="faxNumber">
            <el-input v-model="formData.faxNumber" placeholder="请输入传真号码" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="企业邮箱" prop="email">
            <el-input v-model="formData.email" placeholder="请输入企业邮箱" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入选日期" prop="entryDate">
            <el-date-picker v-model="formData.entryDate" type="date" value-format="YYYY-MM-DD" style="width: 100%"
              placeholder="选择入选日期" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态" prop="status">
            <el-select v-model="formData.status" placeholder="请选择状态">
              <el-option v-for="dict in getIntDictOptions(DICT_TYPE.MAINTENANCE_STATUS)" :key="dict.value"
                :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开户行" prop="bankName">
            <el-input v-model="formData.bankName" placeholder="请输入开户行" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="账号" prop="bankAccount">
            <el-input v-model="formData.bankAccount" placeholder="请输入账号" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="税号" prop="taxNumber">
            <el-input v-model="formData.taxNumber" placeholder="请输入税号" />
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="发票抬头" prop="invoiceTitle">
            <el-input type="textarea" maxlength="200" show-word-limit v-model="formData.invoiceTitle"
              placeholder="请输入发票抬头" />
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="注册地址" prop="registeredAddress">
            <el-input type="textarea" maxlength="200" show-word-limit v-model="formData.registeredAddress"
              placeholder="请输入注册地址" />
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="公司地址" prop="companyAddress">
            <el-input type="textarea" maxlength="200" show-word-limit v-model="formData.companyAddress"
              placeholder="请输入公司地址" />
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="经营范围" prop="businessScope">
            <el-input type="textarea" maxlength="2000" show-word-limit v-model="formData.businessScope"
              placeholder="请输入经营范围" />
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="企业信息" prop="enterpriseInformation">
            <Editor v-model="formData.enterpriseInformation" height="250px" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { MaintenanceInformationApi, MaintenanceInformation } from '@/api/business/maintenanceinformation'

/** 维保商档案 表单 */
defineOptions({ name: 'MaintenanceInformationForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  enterpriseName: undefined,
  simpleName: undefined,
  creditCode: undefined,
  enterpriseCode: undefined,
  legalRepresentative: undefined,
  companyWebsite: undefined,
  contactPerson: undefined,
  mobilePhone: undefined,
  landlinePhone: undefined,
  faxNumber: undefined,
  email: undefined,
  entryDate: undefined,
  status: undefined,
  bankName: undefined,
  bankAccount: undefined,
  taxNumber: undefined,
  invoiceTitle: undefined,
  registeredAddress: undefined,
  companyAddress: undefined,
  businessScope: undefined,
  enterpriseInformation: undefined
})
const formRules = reactive({
  enterpriseName: [{ required: true, message: '企业名称不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      const data = await MaintenanceInformationApi.getMaintenanceInformation(id)

      formData.value = {
        ...data,
        enterpriseInformation: data.enterpriseInformation || "" // 确保不为null 防止富文本组件出错
      };

    } catch (e) {
      console.log(e);

    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MaintenanceInformation
    if (formType.value === 'create') {
      await MaintenanceInformationApi.createMaintenanceInformation(data)
      message.success(t('common.createSuccess'))
    } else {
      await MaintenanceInformationApi.updateMaintenanceInformation(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    enterpriseName: undefined,
    simpleName: undefined,
    creditCode: undefined,
    enterpriseCode: undefined,
    legalRepresentative: undefined,
    companyWebsite: undefined,
    contactPerson: undefined,
    mobilePhone: undefined,
    landlinePhone: undefined,
    faxNumber: undefined,
    email: undefined,
    entryDate: undefined,
    status: undefined,
    bankName: undefined,
    bankAccount: undefined,
    taxNumber: undefined,
    invoiceTitle: undefined,
    registeredAddress: undefined,
    companyAddress: undefined,
    businessScope: undefined,
    enterpriseInformation: undefined
  }
  formRef.value?.resetFields()
}
</script>
